package com.qf.service.impl;

import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.StrategyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 路由策略
 *
 * @Author ztf
 * @Date 19:45 2021/11/24
 **/
@Service(value = "router")
@Slf4j
public class RouterServiceImpl implements StrategyService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AmqpAdmin amqpAdmin;

    /**
     * 策略方法
     *
     * @param standardSubmit
     * @return
     */
    @Override
    public boolean execute(StandardSubmit standardSubmit) {
        log.info("【策略模块-路由策略】，策略开始执行");
        //1. 创建队列
        String queueName = RabbitMQConstants.TOPIC_SMS_GATEWAY + standardSubmit.getGatewayID();
        amqpAdmin.declareQueue(QueueBuilder.durable(queueName).build());

        //2. 发送消息到指定队列
        rabbitTemplate.convertAndSend(queueName,standardSubmit);
        //3. 记录日志
        log.info("【策略模块-路由策略】，执行完成，短信发送成功：standardSubmit={}",standardSubmit);
        return true;
    }
}
